using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;

namespace IWS.Lib.Utilities.Web
{
	public delegate void EditItemEventHandler(EditItemEventArgs e);

	public class ListControl : System.Web.UI.UserControl
	{
		protected bool IsInitialized;
		public ListControl()
			: base()
		{
			IsInitialized = false;
		}

		#region Custom Events
		private event EventHandler eNewItemClick;
		private event EditItemEventHandler eEditItemClick;
		private event EventHandler eRefreshList;

		/// <summary>
		/// Occurs when a new button is clicked
		/// </summary>
		public event EventHandler NewItemClick
		{
			add {this.eNewItemClick += value;}
			remove{this.eNewItemClick -= value;}
		}
		/// <summary>
		/// Occurs after an edit button is clicked.
		/// </summary>
		public event EditItemEventHandler EditItemClick
		{
			add { this.eEditItemClick += value; }
			remove { this.eEditItemClick -= value; }
		}
		/// <summary>
		/// Occurs when a refresh button is clicked.
		/// </summary>
		public event EventHandler RefreshList
		{
			add { this.eRefreshList += value; }
			remove { this.eRefreshList -= value; }
		}

		protected virtual void OnNewItemClick(object sender, EventArgs e)
		{
			if (this.eNewItemClick != null)
			{
				this.eNewItemClick(sender, e);
			}
		}
		protected virtual void OnEditItemClick(object ID)
		{
			if (this.eEditItemClick != null)
			{
				this.eEditItemClick(new EditItemEventArgs(ID));
			}
		}
		protected virtual void OnRefreshList(object sender, EventArgs e)
		{
			if (this.eRefreshList != null)
			{
				this.eRefreshList(sender, e);
			}
		}
		#endregion


		/// <summary>
		/// Initializes this class. 
		/// </summary>
		/// <remarks>This function is the primaty one intended for initialization. This one will call into InitializeCustom only once per postback. In Load functions, you should always call this function first to prevent running initialization code more than once. Override the InitializeCustom for custom implementation.</remarks>
		protected void Initialize()
		{
			if (!IsInitialized)
			{
				IsInitialized = true;
				this.InitializeCustom();
			}
		}

		/// <summary>
		/// Iimplementation of initialization goes here. 
		/// </summary>
		protected virtual void InitializeCustom()
		{
			//perform initialization code here. 
		}

		/// <summary>
		/// Log an exception to a logging utility. 
		/// </summary>
		/// <remarks>This function proxies the log functionality in case the logging library is changed in the future. Form controls should use this as their primary logging mechanism.</remarks>
		/// <param name="ex">Exception to log</param>
		/// <param name="assembly">The assembly which the log originates. (Typically System.Reflection.Assembly.GetExecutingAssembly()).</param>
		protected void LogException(Exception ex, System.Reflection.Assembly assembly)
		{
			//TODO:	customer log info here
		}
		/// <summary>
		/// Change the current selection in a drop down to a new value.
		/// </summary>
		/// <param name="ddl"></param>
		/// <param name="value"></param>
		protected void SetDropDownListValue(DropDownList ddl, string value)
		{
			ListItem item = ddl.Items.FindByValue(value);
			if (item != null)
			{
				ddl.ClearSelection();
				item.Selected = true;
			}
		}
		/// <summary>
		/// Populate the drop down list with data from an IEnumerable object
		/// </summary>
		/// <param name="ddl"></param>
		/// <param name="data"></param>
		/// <param name="text"></param>
		/// <param name="value"></param>
		protected void SetDropDownListData(DropDownList ddl, System.Collections.IEnumerable data, string text, string value)
		{
			ddl.DataSource = data;
			ddl.DataTextField = text;
			ddl.DataValueField = value;
			ddl.DataBind();
		}
		/// <summary>
		/// Populate the drop down list with data from a specific enum using its text value as the text display.
		/// </summary>
		/// <param name="ddl"></param>
		/// <param name="enum_type"></param>
		protected void SetDropDownListData(DropDownList ddl, Type enum_type)
		{
			ddl.Items.Clear();
			foreach (System.Enum eValue in Enum.GetValues(enum_type))
			{
				int value = Convert.ToInt32(eValue);
				ddl.Items.Add(new ListItem(eValue.ToString(), value.ToString()));
			}
		}
	}
	public class EditItemEventArgs : EventArgs
	{
		public EditItemEventArgs() { }
		public EditItemEventArgs(object ID)
		{
			this.ItemID = ID;
		}
		private object m_oItemID;

		public object ItemID
		{
			get { return m_oItemID; }
			set { m_oItemID = value; }
		}
	}
}
